package com.apache.uct.service.plugins;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.apache.api.manager.BaseManager;
import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.uct.common.entity.Act;
import com.apache.uct.common.entity.ActGive;
import com.apache.uct.common.entity.Org;
import com.apache.uct.manager.ActGiveManager;
import com.apache.uct.manager.ActManager;
import com.apache.uct.manager.OrgManager;

/**
 * description:  机构管理插件  
 * @author dyhou 创建时间：2016-8-18
 */
public class OrgActionPluginImpl extends AbstractSuperPlugin {

	private OrgManager orgManager;
	private ProxyManager uctProxyManager;
	private ActManager actManager;
	private ActGiveManager actGiveManager;

	public Object execute(ParamsVo param) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");
		String method = String.valueOf(param.getParams("method"));
		if ("orgSave".equals(method)) {
			result = save(result, param);
		} else if ("orgDel".equals(method)) {
			result = delete(result, param);
		} else if ("orgActData".equals(method)) {
			result = orgActData(result, param);
		}
		return result;
	}

	private ResultEntity save(ResultEntity result, ParamsVo param) {
		ParamsVo<Org> vo = new ParamsVo<Org>();
		Org item = new Org();
		item = (Org) ClassToolsUtil.getInstance().convert2Bean(param.getParams(), item);
		if (Validator.isNull(item.getFatherId()))
			item.setFatherId("0");
		long times = DateUtils.toLong(new Date());
		String userEname = String.valueOf(param.getParams("nowUserEname"));
		if (Validator.isNotNull(item.getOrgId())) {//对机构信息修改
			if (!"0".equals(item.getFatherId())) {
				ParamsVo pvo = new ParamsVo();
				pvo.setInfoId(item.getFatherId());
				Org org = (Org) orgManager.getInfoById(pvo);
				item.setFullEname(org.getFullEname() + "/" + item.getOrgEname());
				item.setFullCname(org.getOrgCname() + "/" + item.getOrgCname());
			} else {
				item.setFullEname("/" + item.getOrgEname());
				item.setFullCname("/" + item.getOrgCname());
			}
			item.setUpdateTime(times);
			int updateCount = item.getUpdateCount() == null ? 0 : item.getUpdateCount();
			item.setUpdateCount(updateCount + 1);
			item.setUpdateUser(userEname);

		} else {//添加机构信息
			if ("1".equals(SystemTools.getInstance().getValue("org_act_switch"))) {//是否开启授权
				if (!"1".equals(StrUtil.doNull(String.valueOf(param.getParams("nowSysUser")), "0"))
						&& "0".equals(item.getFatherId())) {//是否为系统用户
					result.setMessage("您没有此项操作权限！");
					return result;
				}
			}
			if (!"0".equals(item.getFatherId())) {
				ParamsVo pvo = new ParamsVo();
				pvo.setInfoId(item.getFatherId());
				Org org = (Org) orgManager.getInfoById(pvo);
				item.setFullEname(org.getFullEname() + "/" + item.getOrgEname());
				item.setFullCname(org.getOrgCname() + "/" + item.getOrgCname());
			} else {
				item.setFullEname("/" + item.getOrgEname());
				item.setFullCname("/" + item.getOrgCname());
			}
			item.setCreateUser(userEname);
			item.setSysEname("uct");
			item.setUpdateCount(0);
			item.setCreateTime(times);
			item.setRegTime(times);
			vo.setObj(item);
			vo.setKey("checkOrgEname");
			String flag = String.valueOf(orgManager.execute(vo));
			if (!"true".equals(flag)) {
				result.setMessage("机构标识[" + item.getOrgEname() + "]已经被使用");
				return result;
			}
		}
		vo.setObj(item);
		ResultMsg msg = super.save(vo, item.getOrgId());
		if ("T".equals(msg.getFlag())) {
			result.setEntity(msg.getFlag());
			result.setMessage(msg.getFlag());
		} else {
			result.setMessage(msg.getFlag());
		}
		return result;
	}

	private ResultEntity delete(ResultEntity result, ParamsVo param) {
		ParamsVo<Org> vo = new ParamsVo<Org>();
		String id = String.valueOf(param.getParams("orgId"));
		if (Validator.isNotNull(id)) {
			vo.setKey("deleteOrg");
			vo.setParams("infoType", "org");
			String msg = String.valueOf(uctProxyManager.doInvoke(vo));
			if (Validator.isNotNull(msg)) {
				result.setMessage("删除失败！");
				return result;
			}
		} else {
			result.setMessage("请选择要删除的机构！");
		}
		result.setEntity("T");
		result.setMessage("删除机构成功！");
		return result;
	}

	/**
	 * 获取权限分配列表
	 */
	private ResultEntity orgActData(ResultEntity result, ParamsVo param) {
		String parentId = StrUtil.doNull(String.valueOf(param.getParams("id")), "0");
		String orgId = String.valueOf(param.getParams("orgId"));
		Act actVo = new Act();
		actVo.setFatherId(parentId);
		ParamsVo<Act> vo = new ParamsVo<Act>();
		vo.setObj(actVo);
		String nowOrgId = String.valueOf(param.getParams("nowOrgId"));
		if (!Validator.isNull(nowOrgId) && "1".equals(SystemTools.getInstance().getValue("org_act_switch"))) {
			vo.setParams("userOrgId", nowOrgId);
			vo.setParams("sysUser", StrUtil.doNull(String.valueOf(param.getParams("nowSysUser")), "0"));//是否为系统用户
		}
		String objType = StrUtil.doNull(String.valueOf(param.getParams("objType")), "org");
		List<Act> actList = actManager.getList(vo);
		ParamsVo<ActGive> agVo = new ParamsVo<ActGive>();
		agVo.setParams("roleId", orgId);
		agVo.setParams("giveType", objType);
		agVo.setKey("GiveListByOrgId");
		List<ActGive> giveList = (List<ActGive>) actGiveManager.execute(agVo);
		JSONArray jary = new JSONArray();
		for (int i = 0; i < actList.size(); i++) {
			Act act = actList.get(i);
			JSONObject jvo = JSONObject.fromObject(act);
			if (act.getSubCount() != null && act.getSubCount() > 0) {
				jvo.put("state", "closed");
			} else {
				jvo.put("state", "opened");
			}
			String hasGive = hasGive(giveList, act.getActId()) ? "T" : "F";
			jvo.put("hasGive", hasGive);
			jary.add(jvo);
		}
		result.setEntity(jary);
		result.setMessage("查询成功");
		return result;
	}

	protected BaseManager getBaseManager() {
		return orgManager;
	}

	public void setOrgManager(OrgManager orgManager) {
		this.orgManager = orgManager;
	}

	public void setUctProxyManager(ProxyManager uctProxyManager) {
		this.uctProxyManager = uctProxyManager;
	}

	public void setActManager(ActManager actManager) {
		this.actManager = actManager;
	}

	public void setActGiveManager(ActGiveManager actGiveManager) {
		this.actGiveManager = actGiveManager;
	}

}
